/**
 * Created by Administrator on 2014/12/24.
 */

package com.smartrobot.smartrobot.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.util.Message;

import java.util.ArrayList;
import java.util.List;


public class ChatListViewAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{


    private Context context;
    private List<Message> msgList;
    private final int ME = 0;
    private final int OTHER = 1;
    private  int idf  = 1;
    private int maxMsgLength = 100;

    public ChatListViewAdapter(Context context,List<Message>list){

        this.context = context;
        this.msgList = new ArrayList<Message>();
        this.msgList = list;
//        this.msgList.add(new Message("1","head.png","叶罗均","123456789","吃饭没",""));
//        this.msgList.add(new Message("2","head.png","刘晓东","123456789","我已经吃了","12月15号"));
//        this.msgList.add(new Message("1","head.png","叶罗均","123456789","吃饭没","12月16号"));
//        this.msgList.add(new Message("2","head.png","刘晓东","123456789","我已经吃了","12月15号"));
//        this.msgList.add(new Message("1","head.png","叶罗均","123456789","吃饭没","12月16号"));
//        this.msgList.add(new Message("2","head.png","刘晓东","123456789","我已经吃了","12月15号"));
//        this.msgList.add(new Message("1","head.png","叶罗均","123456789","吃饭没","12月16号"));
//        this.msgList.add(new Message("2","head.png","刘晓东","123456789","我已经吃了","12月15号"));
//        this.msgList.add(new Message("1","head.png","叶罗均","123456789","吃饭没","12月16号"));
//        this.msgList.add(new Message("2","head.png","刘晓东","123456789","我已经吃了","12月15号"));


    }



    @Override
    public int getCount() {
       return msgList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        int type = getItemViewType(position);

        try{
            ViewHolder holder;
            if(view == null){
                //新建一个holder
                holder = new ViewHolder();

                switch (type){

                    case ME:
                        view =  LayoutInflater.from(context).inflate(R.layout.page_message_chating_right, viewGroup, false);
                        holder.headImg = (ImageView) view.findViewById(R.id.chating_adapter_right_item_head);
                        holder.say = (TextView)view.findViewById(R.id.chating_adapter_right_item_say);
                        holder.time = (TextView)view.findViewById(R.id.chating_adapter_right_item_time);
                        holder.isSuccess = (ImageView)view.findViewById(R.id.chating_adapter_right_item_isSuccess);

                        break;

                    case OTHER:

                        view =  LayoutInflater.from(context).inflate(R.layout.page_message_chating_left, viewGroup, false);
                        holder.headImg = (ImageView) view.findViewById(R.id.chating_adapter_left_item_head);
                        holder.say = (TextView)view.findViewById(R.id.chating_adapter_left_item_say);
                        holder.time = (TextView)view.findViewById(R.id.chating_adapter_left_item_time);
                        break;

                }
                view.setTag(holder);

            }else{
                holder = (ViewHolder)view.getTag();
            }

            //初始化所有view
            Message msg = msgList.get(position);

            holder.say.setText(msg.getSay());
            holder.time.setText(msg.getTime());



        }catch (Exception e){
            LogToSd.error(e.getMessage());
        }
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {


    }

    @Override
    public int getItemViewType(int position) {

        int type = 0;
        // 重要 这里是本人的id
        if(idf==ME){
            type = ME;
//            idf = 1;
        }else
            type = OTHER;

        return type;
    }

    @Override
    public int getViewTypeCount() {
        return 2;//只有两种不同的布局
    }

    class ViewHolder{
        ImageView headImg;
        TextView say;
        TextView time;
        ImageView isSuccess;
    }


    public void notifyChange(Message message,int arg_idf){
        idf=arg_idf;
        msgList.add(message);
        this.notifyDataSetChanged();
    }


}
